from fastapi import APIRouter, Depends, status

from app.api.deps import (
    CurrentUser,
    SessionDep,
    get_current_active_superuser,
)

from app.core.security import  verify_password
from app import  models
from app import services


router = APIRouter()


@router.get(
    "/",
    dependencies=[Depends(get_current_active_superuser)]
)
async def read_users(session: SessionDep, skip: int = 0, limit: int = 100) -> models.UsersPublicResponse:
    """
    Retrieve users.
    """
    res =  await services.UsersService(current_user=None,db=session).read_users(skip,limit)
    return models.UsersPublicResponse(code=200,data=res,msg="成功")

@router.post(
    "/", dependencies=[Depends(get_current_active_superuser)]
)
async def create_user(*, session: SessionDep, user_in: models.UserCreate) -> models.UserPublicResponse:
    """
    Create new user.
    """
    return await services.UsersService(current_user=None,db=session).create_user(user_in)  

@router.put("/me")
async def update_user_me(
    *, session: SessionDep, user_in: models.UserProfileUpdateMe, current_user: CurrentUser) -> models.UserPublicResponse:
    """
    Update own user.
    """   
    return await services.UserProfileService(current_user=current_user,db=session).update_me(user_in)        

@router.patch("/me/password")
async def update_password_me(
    *, session: SessionDep, body: models.UpdatePassword, current_user: CurrentUser
) -> models.UserPublicResponse:
    """
    Update own password.
    """
    if not verify_password(body.current_password, current_user.hashed_password):
        return models.UserPublicResponse(code=status.HTTP_400_BAD_REQUEST, msg="Incorrect password",data=None)
        
    if body.current_password == body.new_password:
        return models.UserPublicResponse(code=status.HTTP_400_BAD_REQUEST, msg="New password cannot be the same as the current one",data=None)
        
    return await services.UsersService(current_user=current_user,db=session).update_password(body)
    


@router.get("/me", )
def read_user_me(current_user: CurrentUser) -> models.UserPublicResponse:
    """
    Get current user.
    """
    return models.UserPublicResponse(msg="成功",code=0,data=current_user)


@router.get("/me/authinfo")
async def get_user_me_authinfo(session: SessionDep, current_user: CurrentUser) -> models.UserAuthResponse:
    """
    获得当前用户的认证信息
    """
    fileter_str={'userId':current_user.id}            
    return await services.UsersService(current_user=current_user,db=session).get_user_me_authinfo(fileter_str)



@router.delete("/me")
async def delete_user_me(session: SessionDep, current_user: CurrentUser) -> models.UserPublicResponse:
    """
    Delete own user.
    """
    if current_user.is_superuser :
            return models.UserPublicResponse(code=status.HTTP_403_FORBIDDEN, msg="Super users are not allowed to delete themselves",data=None)
            
    return await services.UsersService(current_user=current_user,db=session).delete_me(current_user.id)


# @router.post("/create_profile")
# async def create_profile(session: SessionDep, user_in: models.UserProfileUpdateMe,current_user: CurrentUser) -> models.UserPublicResponse:
#     """
#     用户登陆后，可以创建profile.
#     """
  
#     return await services.UserProfileService(current_user=current_user,db=session).create_profile(user_in)

@router.get("/{user_id}")
async def read_user_by_id(
    user_id: int, session: SessionDep, current_user: CurrentUser
) -> models.UserPublicResponse:
    """
    Get a specific user by id.
    """
    return await services.UsersService(current_user=current_user,db=session).read_users_by_id(user_id)

@router.patch("/{user_id}",dependencies=[Depends(get_current_active_superuser)])
async def update_user(*,session: SessionDep,user_id: int,user_in: models.UserProfileUpdateMe) -> models.UserPublicResponse:
    """
    Update a user.
    """   
   
    return await services.UsersService(current_user=None,db=session).update_user_by_id(user_id,user_in)      
    


@router.delete("/{user_id}", dependencies=[Depends(get_current_active_superuser)])
async def delete_user(
    session: SessionDep, current_user: CurrentUser, user_id: int
) -> models.UserPublicResponse:
    """
    Delete a user.
    """
  
    return await services.UsersService(current_user=current_user,db=session).delete_user(user_id)      
   